package main

import (
	"fmt"
	"os"
	"time"
	github "zzh.com/study/demo/ch4/ch4.5"
)

type class string

const (
	LESS1MOMTH class = "less than one month"
	LESS1YEAR  class = "less than one year"
	MORE1YEAR  class = "more than one year"
)

func main() {
	data := make(map[class][]*github.Issue)
	result, err := github.SearchIssues(os.Args[1:])
	now := time.Now()
	oneMonthAgo := now.AddDate(0, -1, 0)
	oneYearAgo := now.AddDate(-1, 0, 0)
	if err != nil {
		fmt.Errorf("search query failed: %s", err)
	}
	for _, v := range result.Items {
		createAt := v.CreatedAt
		switch {
		case oneMonthAgo.Before(createAt):
			data[LESS1MOMTH] = append(data[LESS1MOMTH], v)
		case oneYearAgo.Before(createAt):
			data[LESS1YEAR] = append(data[LESS1YEAR], v)
		case oneYearAgo.After(createAt):
			data[MORE1YEAR] = append(data[MORE1YEAR], v)
		}
	}

	for k, v := range data {
		fmt.Printf("%s:%s", k, v)
	}
}
